package com.wannengbang.cloudleader.service;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.SkeletonScreen;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.ExchangeListBean;
import com.wannengbang.cloudleader.bean.PlanListBean;
import com.wannengbang.cloudleader.dialog.ChooseCardDialog;
import com.wannengbang.cloudleader.event.PlanEvent;
import com.wannengbang.cloudleader.service.adapter.PlanListAdapter;
import com.wannengbang.cloudleader.service.model.IServiceModel;
import com.wannengbang.cloudleader.service.model.ServiceModelImpl;
import com.wannengbang.cloudleader.utils.ToastUtil;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.WrapContentLinearLayoutManager;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PlanListActivity extends BaseActivity implements OnRefreshLoadMoreListener {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.ll_add_plan)
    LinearLayout llAddPlan;
    private IServiceModel serviceModel;
    private PlanListAdapter listAdapter;
    private List<PlanListBean.DataBean> beanList;
    private SkeletonScreen skeletonScreen;
    private int page = 1;
    private String card_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_plan_list);
        ButterKnife.bind(this);
        registerEventBus();
        initView();
    }

    public void initView() {
        card_id = getIntent().getStringExtra("card_id");
        refreshLayout.setRefreshHeader(new ClassicsHeader(mActivity));//设置Header
        refreshLayout.setRefreshFooter(new ClassicsFooter(mActivity));//设置Footer
        refreshLayout.setOnRefreshLoadMoreListener(this);
        serviceModel = new ServiceModelImpl();
        titleBar.setRightClickListener(() -> {
            ChooseCardDialog dialog = ChooseCardDialog.newInstance();
            dialog.show(getSupportFragmentManager(), "dialog");
            dialog.setOnCallBackListener((name, chooseId) -> {
                card_id = chooseId;
                refreshData();
            });
        });
        beanList = new ArrayList<>();
        listAdapter = new PlanListAdapter(beanList);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);
        listAdapter.setOnCallBackListener((type, position) -> {
            if (Double.parseDouble(beanList.get(position).getDelete_time()) > 0) {
                if (beanList.get(position).getRepayment_status() == 2) {
                    Intent intent = new Intent(mActivity, PlanDetailsActivity.class);
                    intent.putExtra("plan_id", beanList.get(position).getId() + "");
                    startActivity(intent);
                } else {
                    ToastUtil.showShort("此计划已作废，请重新添加计划");
                }
            } else {
                Intent intent = new Intent(mActivity, PlanDetailsActivity.class);
                intent.putExtra("plan_id", beanList.get(position).getId() + "");
                startActivity(intent);
            }

        });

        skeletonScreen = Skeleton.bind(recyclerView)
                .adapter(listAdapter)
                .color(R.color.shimmer_color_2)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1000)
                .count(20)
                .load(R.layout.item_skeleton_product_list)
                .show();
        requestPlanIndex();
    }

    @Subscribe
    public void planEvent(PlanEvent event) {
        refreshData();
    }

    private void requestPlanIndex() {
        serviceModel.requestPlanIndex(card_id, new DataCallBack<PlanListBean>() {
            @Override
            public void onSuccessful(PlanListBean bean) {
                if (bean.getData() != null && bean.getData().size() > 0) {
                    beanList.addAll(bean.getData());
                    if (page == 1) {
                        refreshLayout.finishRefresh();
                    } else {
                        refreshLayout.finishLoadMore();
                    }
                } else {
                    if (page != 1) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.finishRefresh();
                    }
                }
                if (beanList.size() > 0) {
                    llNoData.setVisibility(View.GONE);
                } else {
                    llNoData.setVisibility(View.VISIBLE);
                }
                listAdapter.notifyDataSetChanged();
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }

            @Override
            public void onFailed(String msg, String code) {
                skeletonScreen.hide();
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        requestPlanIndex();
        refreshLayout.finishLoadMore(30000);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshData();
    }

    public void refreshData() {
        page = 1;
        beanList.clear();
        requestPlanIndex();
        refreshLayout.finishRefresh(30000);
    }

    @OnClick(R.id.ll_add_plan)
    public void onViewClicked() {
        Intent intent = new Intent(mActivity, AddPlanActivity.class);
        intent.putExtra("card_id", card_id);
        startActivity(intent);
    }
}
